/*
 * Copyright (c) 2023. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.y5neko.sec.classloader;

import java.lang.reflect.Method;

/**
 * @author Y5neKO
 */
public class TestClassLoader extends ClassLoader {
    // TestHelloWorld类名
    public static String TEST_CLASS_NAME = "com.y5neko.sec.classloader.TestHelloWorld";

    //TestHelloWorld类的字节码
    public static final byte[] TEST_CLASS_BYTES = new byte[]{-54, -2, -70, -66, 0, 0, 0, 52, 0, 47, 10, 0, 10, 0, 26, 10, 0, 10, 0, 27, 10, 0, 28, 0, 29, 8, 0, 30, 10, 0, 31, 0, 32, 7, 0, 33, 10, 0, 6, 0, 34, 8, 0, 35, 7, 0, 36, 7, 0, 37, 1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 1, 0, 15, 76, 105, 110, 101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0, 18, 76, 111, 99, 97, 108, 86, 97, 114, 105, 97, 98, 108, 101, 84, 97, 98, 108, 101, 1, 0, 4, 116, 104, 105, 115, 1, 0, 43, 76, 99, 111, 109, 47, 121, 53, 110, 101, 107, 111, 47, 115, 101, 99, 47, 99, 108, 97, 115, 115, 108, 111, 97, 100, 101, 114, 47, 84, 101, 115, 116, 72, 101, 108, 108, 111, 87, 111, 114, 108, 100, 59, 1, 0, 5, 104, 101, 108, 108, 111, 1, 0, 20, 40, 41, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 1, 0, 1, 101, 1, 0, 21, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 69, 120, 99, 101, 112, 116, 105, 111, 110, 59, 1, 0, 13, 83, 116, 97, 99, 107, 77, 97, 112, 84, 97, 98, 108, 101, 7, 0, 33, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108, 101, 1, 0, 19, 84, 101, 115, 116, 72, 101, 108, 108, 111, 87, 111, 114, 108, 100, 46, 106, 97, 118, 97, 12, 0, 11, 0, 12, 12, 0, 38, 0, 39, 7, 0, 40, 12, 0, 41, 0, 42, 1, 0, 41, 99, 111, 109, 46, 121, 53, 110, 101, 107, 111, 46, 115, 101, 99, 46, 99, 108, 97, 115, 115, 108, 111, 97, 100, 101, 114, 46, 84, 101, 115, 116, 72, 101, 108, 108, 111, 87, 111, 114, 108, 100, 7, 0, 43, 12, 0, 44, 0, 45, 1, 0, 19, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 69, 120, 99, 101, 112, 116, 105, 111, 110, 12, 0, 46, 0, 12, 1, 0, 13, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 126, 51, 1, 0, 41, 99, 111, 109, 47, 121, 53, 110, 101, 107, 111, 47, 115, 101, 99, 47, 99, 108, 97, 115, 115, 108, 111, 97, 100, 101, 114, 47, 84, 101, 115, 116, 72, 101, 108, 108, 111, 87, 111, 114, 108, 100, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 1, 0, 8, 103, 101, 116, 67, 108, 97, 115, 115, 1, 0, 19, 40, 41, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 67, 108, 97, 115, 115, 59, 1, 0, 15, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 67, 108, 97, 115, 115, 1, 0, 14, 103, 101, 116, 67, 108, 97, 115, 115, 76, 111, 97, 100, 101, 114, 1, 0, 25, 40, 41, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 67, 108, 97, 115, 115, 76, 111, 97, 100, 101, 114, 59, 1, 0, 21, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 67, 108, 97, 115, 115, 76, 111, 97, 100, 101, 114, 1, 0, 9, 108, 111, 97, 100, 67, 108, 97, 115, 115, 1, 0, 37, 40, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 41, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 67, 108, 97, 115, 115, 59, 1, 0, 15, 112, 114, 105, 110, 116, 83, 116, 97, 99, 107, 84, 114, 97, 99, 101, 0, 33, 0, 9, 0, 10, 0, 0, 0, 0, 0, 2, 0, 1, 0, 11, 0, 12, 0, 1, 0, 13, 0, 0, 0, 47, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 1, -79, 0, 0, 0, 2, 0, 14, 0, 0, 0, 6, 0, 1, 0, 0, 0, 6, 0, 15, 0, 0, 0, 12, 0, 1, 0, 0, 0, 5, 0, 16, 0, 17, 0, 0, 0, 1, 0, 18, 0, 19, 0, 1, 0, 13, 0, 0, 0, 113, 0, 2, 0, 2, 0, 0, 0, 24, 42, -74, 0, 2, -74, 0, 3, 18, 4, -74, 0, 5, 87, -89, 0, 8, 76, 43, -74, 0, 7, 18, 8, -80, 0, 1, 0, 0, 0, 13, 0, 16, 0, 6, 0, 3, 0, 14, 0, 0, 0, 22, 0, 5, 0, 0, 0, 9, 0, 13, 0, 12, 0, 16, 0, 10, 0, 17, 0, 11, 0, 21, 0, 14, 0, 15, 0, 0, 0, 22, 0, 2, 0, 17, 0, 4, 0, 20, 0, 21, 0, 1, 0, 0, 0, 24, 0, 16, 0, 17, 0, 0, 0, 22, 0, 0, 0, 7, 0, 2, 80, 7, 0, 23, 4, 0, 1, 0, 24, 0, 0, 0, 2, 0, 25};

    @Override
    public Class<?> findClass(String name) throws ClassNotFoundException {
        // 只处理TestHelloWorld类
        System.out.println(name);
        if (name.equals(TEST_CLASS_NAME)) {
            // 调用JVM的native方法定义TestHelloWorld类
            return defineClass(TEST_CLASS_NAME, TEST_CLASS_BYTES, 0, TEST_CLASS_BYTES.length);
        }

        //其他类通过super关键字调用父类的findClass方法
        return super.findClass(name);
    }

    public static void main(String[] args) {
        //创建自定义类加载器
        TestClassLoader loader = new TestClassLoader();

        try{
            //使用自定义的类加载器”loader“中的loadClass加载TestHelloWorld类到testClass变量
            Class<?> testClass = loader.loadClass(TEST_CLASS_NAME);
            System.out.println();

            //反射创建TestHelloWorld类，等价于” TestHelloWorld t = new TestHelloWorld();“
            Object testInstance = testClass.newInstance();

            //反射获取hello方法
            Method method = testInstance.getClass().getMethod("hello");

            //反射调用hello方法，等价于”String str = t.hello();“
            String str = (String) method.invoke(testInstance);

            System.out.println(str);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
